package com.example.video.admin_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.video.admin_server.entity.SysUserRole;
import com.example.video.admin_server.entity.SysUser;
import com.example.video.admin_server.entity.dto.SysUserDto;
import com.example.video.admin_server.entity.vo.SysUserDetailVo;
import com.example.video.admin_server.mapper.SysUserMapper;
import com.example.video.admin_server.service.ISysUserRoleService;
import com.example.video.admin_server.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台管理员表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private ISysUserRoleService iSysUserRoleService;

    @Override
    @Transactional
    public boolean add(SysUserDto sysUserDto) {
        boolean save = this.save(sysUserDto);
        boolean b = addUserRole(sysUserDto);
        return save && b;
    }

    @Override
    @Transactional
    public boolean update(SysUserDto sysUserDto) {
        boolean update = this.updateById(sysUserDto);
        boolean a = addUserRole(sysUserDto);
        boolean b = delUserRole(sysUserDto);
        return update && a && b;
    }

    @Override
    @Transactional
    public boolean delUser(Integer id) {
        boolean b = this.removeById(id);
        boolean remove = iSysUserRoleService.remove(new QueryWrapper<>(new SysUserRole().setUserId(id)));
        return b && remove;
    }

    @Override
    public SysUserDetailVo getDetails(Integer id) {
        SysUser byId = this.getById(id);
        List<SysUserRole> list = iSysUserRoleService.list(new QueryWrapper<>(new SysUserRole().setUserId(id)));
        List<Integer> roleIds = new ArrayList<>();
        if (list.size() > 0){
            for (SysUserRole sysUserRole : list){
                roleIds.add(sysUserRole.getRoleId());
            }
        }
        SysUserDetailVo sysUserDetailVo = new SysUserDetailVo(byId);
        sysUserDetailVo.setCheckRoleIds(roleIds);
        return sysUserDetailVo;
    }

    private boolean addUserRole(SysUserDto sysUserDto){
        List<Integer> roleIds = sysUserDto.getAddRoleIds();
        boolean add = true;
        if (roleIds != null && roleIds.size() > 0 ) {
            List<SysUserRole> sysUserRoles = new ArrayList<>();
            SysUserRole sysUserRole;
            for (Integer roleId : roleIds) {
                sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(roleId).setUserId(sysUserDto.getId());
                sysUserRoles.add(sysUserRole);
            }
            add = iSysUserRoleService.saveBatch(sysUserRoles);
        }
        return add;
    }

    private boolean delUserRole(SysUserDto sysUserDto){
        List<Integer> roleIds = sysUserDto.getDelRoleIds();
        boolean del = true;
        if (roleIds != null && roleIds.size() > 0 ) {
            List<SysUserRole> list = iSysUserRoleService.list(new QueryWrapper<>(new SysUserRole().setUserId(sysUserDto.getId())));
            List<Integer> delIds = new ArrayList<>();
            for (SysUserRole sysUserRole : list) {
                if (roleIds.contains(sysUserRole.getRoleId())){
                    delIds.add(sysUserRole.getId());
                }
            }
            del = iSysUserRoleService.removeByIds(delIds);
        }
        return del;
    }
}
